const Build = plus.android.importClass("android.os.Build");

function addShortcuts(shortcuts) {
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
		const main = plus.android.runtimeMainActivity();
		const shortcutManager = main.getSystemService(plus.android.getAttribute(main, 'SHORTCUT_SERVICE'));

		try {
			const shortcutInfoList = plus.android.newObject('java.util.ArrayList');
			shortcuts.forEach((item) => {
				const intent = plus.android.newObject('android.content.Intent', 'android.intent.action.MAIN');
				plus.android.invoke(intent, 'setClassName', main, "io.dcloud.PandoraEntryActivity");
				plus.android.invoke(intent, 'addFlags', plus.android.getAttribute(intent, 'FLAG_ACTIVITY_NEW_TASK'));
				plus.android.invoke(intent, 'setAction', plus.android.getAttribute(intent, 'ACTION_VIEW'));
				plus.android.invoke(intent, 'putExtra', 'path', item.path);

				const shortcut = plus.android.newObject("android.content.pm.ShortcutInfo$Builder", main, item.id);
				const bitmap = plus.android.invoke('android.graphics.BitmapFactory', 'decodeFile', item.icon);
				const icon = plus.android.invoke('android.graphics.drawable.Icon', 'createWithBitmap', bitmap);

				plus.android.invoke(shortcut, 'setShortLabel', item.shortLabel || item.title);
				plus.android.invoke(shortcut, 'setLongLabel', item.title);
				plus.android.invoke(shortcut, 'setIntent', intent);
				plus.android.invoke(shortcut, 'setIcon', icon);

				plus.android.invoke(shortcutInfoList, 'add', plus.android.invoke(shortcut, 'build'));

				plus.android.autoCollection(icon);
				plus.android.autoCollection(bitmap);
				plus.android.autoCollection(shortcut);
				plus.android.autoCollection(intent);
			})
			const data = plus.android.invoke(shortcutManager, 'setDynamicShortcuts', shortcutInfoList)

			plus.android.autoCollection(shortcutInfoList);
			return data;
		} catch (e) {
			console.log(e);
			return false;
		}
	}
	return false;
}

function removeAll() {
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
		const main = plus.android.runtimeMainActivity();
		const shortcutManager = main.getSystemService(plus.android.getAttribute(main, 'SHORTCUT_SERVICE'));
		const data = plus.android.invoke(shortcutManager, 'removeAllDynamicShortcuts');

		plus.android.autoCollection(shortcutManager);
		return data;
	}
	return false;
}

export {
	addShortcuts,
	removeAll
};
export default {
	addShortcuts,
	removeAll
};